﻿using System;
using System.Data.Entity.Infrastructure;
using System.Linq;
using EntityModel;

namespace DataAccess
{
    public class DataAccessBase
    {
        protected WRSEntities objContext;

        protected DataAccessBase()
        {
            this.objContext = new WRSEntities();
            this.objContext.Configuration.ProxyCreationEnabled = false;
            this.objContext.Configuration.LazyLoadingEnabled = false;
            ((IObjectContextAdapter)this.objContext).ObjectContext.CommandTimeout = Common.Variables.TimeOut;
        }

        public IQueryable<T> GetRepositoryWithoutTracking<T>() where T : EntityBase
        {
            return this.objContext.Set<T>().AsNoTracking();            
        }

        public IQueryable<T> GetRepository<T>() where T : EntityBase
        {
            return this.objContext.Set<T>();
        }

        public T GetOne<T>(UserModel userInfo, params object[] keys) where T : EntityBase
        {
            if (!IsAuthorized<T>(userInfo)) return null;

            return this.objContext.Set<T>().Find(keys);
        }

        public bool Any<T>(UserModel userInfo, Func<T,bool> lamdaExpression) where T : EntityBase
        {
            if (!IsAuthorized<T>(userInfo)) return false;

            return this.objContext.Set<T>().Any(lamdaExpression);
        }

        public bool IsAuthorized<T>(UserModel userInfo) where T : EntityBase
        {
            return true;
        }
    }
}
